<%@ page language="java"
	import="java.util.*,cn.edu.cuit.elena.transaction.system.UserContext;"
	pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP starting page</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext
			.onReady(function() {

				var exportFlag = "null";
				var hdfsfile = "null";
				var b_username = "null";
				var b_url = "null";
				var b_password = "null";
				var b_driver = "null";
				var b_schemaname = "null";
				var flag;
				var hdfsPath = "null";
				var userContext = "<%=((UserContext) session.getAttribute("usercontext")).getIdentifier()%>";
				function exportToLocal() {
					if (hdfsPath =="null"&&hdfsfile == "null") {
						Ext.MessageBox.alert("Prompt", "请选择一张表或者一个HDFS路径!");
						return;
					}
					Ext.MessageBox.prompt("Prompt",
							"请输入一个目标导出路径：", function(button,
									text) {
// 									alert(text);
									if(text==""){
									Ext.MessageBox.prompt("Prompt","请输入导出目标路径.");
									return;
									}
						Ext.MessageBox.prompt("Prompt","请输入导出行数:",function(button,text1){
						
// 								alert(text + " " + hdfsfile);
// alert(text1);
								if(text1==""){
									Ext.MessageBox.alert("Prompt", "导出所有内容.");
								}
											
	var requestConfig = {
									url : 'export.svt?kind=exporttolocal',
									params : {
										filepath : text,
										hdfspath : hdfsfile,
										tablepath:hdfsPath,
										count:text1,
										exportflag:exportFlag
									},
									callback : function(option, success,
											response) {
											if(response.responseText=="1"){
												Ext.MessageBox.alert("Prompt", "成功.");
											}else{
												Ext.MessageBox.alert("Prompt", "错误.");
											}
									}
								}
								Ext.Ajax.request(requestConfig);
						})
							});
				}

				function exportFunction() {
					if (hdfsPath == "null") {
						Ext.MessageBox.alert("Prompt",
								"请选择一张表或一个目录");
						return;
					}

					var db;
					var url;
					var username;
					var password;
					var table;
					Ext.MessageBox
							.prompt(
									"Prompt",
									"请输入数据库类型:",
									function(button, text) {
										if (button == "ok") {
											// 																	alert("您的data vender是:"
											// 																			+ text);
											db = text;
											Ext.MessageBox
													.prompt(
															"Prompt",
															"请输入数据库用户名:",
															function(button,
																	text) {
																if (button == "ok") {
																	// 																							alert("您的新年username:"
																	// 																									+ text);
																	username = text;
																	Ext.MessageBox
																			.prompt(
																					"Prompt",
																					"请输入数据库密码:",
																					function(
																							button,
																							text) {
																						if (button == "ok") {
																							// 																													alert("您的新password是:"
																							// 																															+ text);
																							password = text;

																							Ext.MessageBox
																									.prompt(
																											"Prompt",
																											"请输入目标表名:",
																											function(
																													button,
																													text) {
																												if (button == "ok") {
																													// 																																			alert("您的table name年愿望是:"
																													// 																																					+ text);
																													table = text;

																													Ext.MessageBox
																															.prompt(
																																	"Prompt",
																																	"请输入URL链接字符串:",
																																	function(
																																			button,
																																			text) {
																																		if (button == "ok") {
																																			// 																																			alert("您的table name年愿望是:"
																																			// 																																					+ text);
																																			url = text;
																																			///////////////////////////////////////////////////////////     export
																																			// 																																			alert(db
																																			// 																																					+ url
																																			// 																																					+ username
																																			// 																																					+ password
																																			// 																																					+ table
																																			// 																																					+hdfsPath);

																																			var requestConfig = {
																																				url : 'export.svt?kind=export',
																																				params : {
																																					db : db,
																																					url : url,
																																					username : username,
																																					password : password,
																																					table : table,
																																					hdfspath : hdfsPath
																																				},
																																				callback : function(
																																						option,
																																						success,
																																						response) {
																																					var msg = [
																																							success,
																																							option.customer,
																																							response.responseText];
																																					if (response.responseText == "1") {
																																						Ext.MessageBox
																																								.alert(
																																										"Prompt",
																																										"任务启动.");
																																					} else {
																																						Ext.MessageBox
																																								.alert(
																																										"Prompt",
																																										"错误.");
																																					}
																																				}
																																			}
																																			Ext.Ajax
																																					.request(requestConfig);

																																		} else
																																			alert("你放弃了录入！");
																																		return;
																																	});

																												} else
																													alert("你放弃了录入！");
																												return;
																											});

																						} else
																							alert("你放弃了录入！");
																						return;
																					});
																} else
																	alert("你放弃了录入！");
																return;
															});
										} else
											alert("你放弃了录入！");
										return;
									});

				};

				// 				var tree = new Ext.tree.TreePanel({
				// 					animate : true,
				// 					frame : true,
				// 					region : 'center',
				// 					title : 'HDFS Directory',
				// 					margins : '5 0 5 5',
				// 					rootVisible : true,
				// 					autoScroll : true,
				// 					width : 300,
				// 									tbar : [new Ext.Button({
				// 										text : 'Export To Database',
				// 										handler : exportFunction
				// 									}), {
				// 										xtype : 'tbseparator'
				// 									}, new Ext.Button({
				// 										text : 'Export To Local',
				// 										handler : exportToLocal
				// 									})],
				// 									lines : true,
				// 					loader : new Ext.tree.TreeNode({
				// 						dataUrl : 'hdfsview.svt?kind=hdfs'
				// 					}),
				// 					root : new Ext.tree.AsyncTreeNode({
				// 						text : 'ROOT',
				// 						id : 'root',
				// 						expanded : true
				// 					})
				// 				});

				var tree = new Ext.tree.TreePanel({
					region : 'center',
					title : '分布式文件系统目录',
					frame : true,
					autoScroll : true,
					margins : '5 0 5 5',
					tbar : [new Ext.Button({
						text : '导出到数据库',
						handler : exportFunction
					}), {
						xtype : 'tbseparator'
					}, new Ext.Button({
						text : '导出到本地',
						handler : exportToLocal
					})],
					lines : true,
					height : 300,
					loader : new Ext.tree.TreeLoader({
						dataUrl : 'hdfsview.svt?kind=hdfs'
					}),
					root : new Ext.tree.AsyncTreeNode({
						text : 'HDFS Directory'
					})
				});

				tree.on("click", function(tree, root, s) {
					if (tree.leaf != "true")
						return;
					var p_1 = tree.parentNode;
					var p_2 = p_1.parentNode;
					hdfsPath = "/user/quintus/" + userContext + "/" + p_2.text
							+ "/" + p_1.text;
							
					hdfsfile = "/user/quintus/" + userContext + "/" + p_2.text
							+ "/" + p_1.text+"/"+tree.text;
					// 					alert(hdfsPath);
					exportFlag = "1";
				});

				var proxy_1 = new Ext.data.HttpProxy({
					url : 'scriptquery.svt?kind=showtables'
				});

				var reader_1 = new Ext.data.JsonReader({}, [{
					name : 'tablename'
				}, {
					name : 'hivetablename'
				}, {
					name : 'hdfspath'
				}]);

				var store_1 = new Ext.data.Store({
					proxy : proxy_1,
					reader : reader_1
				});

				store_1.load();

				var tables = new Ext.grid.GridPanel({
					store : store_1,
					margins : '5 0 5 5',
					title : 'Hive表',
					frame : true,
					region : 'west',
					width : 370,
					tbar : [new Ext.Button({
						text : '导出到数据库',
						handler : exportFunction
					}), {
						xtype : 'tbseparator'
					}, new Ext.Button({
						text : '导出到本地',
						handler : exportToLocal
					})],
					columns : [{
						id : 'tablename',
						header : '数据库表明',
						width : 150,
						sortable : true,
						dataIndex : 'tablename',
						align : 'center'
					}, {
						id : 'hivetablename',
						header : 'Hive表名',
						width : 200,
						sortable : true,
						dataIndex : 'hivetablename',
						align : 'center'
					}, {
						id : 'hdfspath',
						header : 'HDFS目录',
						width : 200,
						sortable : true,
						dataIndex : 'hdfspath',
						align : 'center'
					}]
				});

				var proxy_5 = new Ext.data.HttpProxy({
					url : 'export.svt?kind=tasks'
				});

				var reader_5 = new Ext.data.JsonReader({}, [{
					name : 'taskId'
				}, {
					name : 'username'
				}, {
					name : 'password'
				}, {
					name : 'url'
				}, {
					name : 'progress'
				}]);

				var store_5 = new Ext.data.Store({
					proxy : proxy_5,
					reader : reader_5
				});

				store_5.load();

				var tasks = new Ext.grid.GridPanel({
					store : store_5,
					region : 'east',
					frame : true,
					margins : '5 0 5 5',
					title : '任务面板',
					width : 400,
					tbar : [new Ext.Button({
						text : '刷新',
						handler : function() {

							var requestConfig = {
								url : 'export.svt?kind=refresh',
								params : {},
								callback : function(option, success, response) {
								}
							}
							Ext.Ajax.request(requestConfig);

						}
					})],

					columns : [{
						id : 'taskid',
						header : '任务ID',
						width : 150,
						sortable : true,
						dataIndex : 'taskId',
						align : 'center'
					}, {
						id : 'username',
						header : '用户名',
						//width : 90,
						sortable : true,
						dataIndex : 'username',
						width : 100,
						align : 'center'
					}, {
						id : 'password',
						header : '密码',
						width : 100,
						sortable : true,
						dataIndex : 'password',
						align : 'center'
					}, {
						id : 'url',
						header : '链接字符串',
						width : 200,
						sortable : true,
						dataIndex : 'url',
						align : 'center'
					}, {
						id : 'progress',
						header : 'Progress',
						//width : 90,
						sortable : true,
						dataIndex : 'progress',
						align : 'center'
					}]
				});
				self.setInterval(function() {
					tasks.getStore().reload();
				}, 1000);

				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [tables, tree, tasks]
				});

				tables.addListener('cellclick', win_2_cellclick);

				var t_schemaname;

				function win_2_cellclick(grid, rowIndex, columnIndex, e) {
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					hdfsPath = selectRows[0].get("hdfspath");
					exportFlag="2";
// 					alert(hdfsPath);
				}
			});
</script>
</head>

<body>
</body>
</html>
